package com.ruoyi.system.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SkplAnswer;
import com.ruoyi.system.service.ISkplAnswerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 回答Controller
 *
 * @author ruoyi
 * @date 2022-06-15
 */
@RestController
@RequestMapping("/system/answer")
public class SkplAnswerController extends BaseController
{
    @Autowired
    private ISkplAnswerService skplAnswerService;

    /**
     * 查询回答列表
     */
    @PreAuthorize("@ss.hasPermi('system:answer:list')")
    @GetMapping("/list")
    public TableDataInfo list(SkplAnswer skplAnswer)
    {
        startPage();
        List<SkplAnswer> list = skplAnswerService.selectSkplAnswerList(skplAnswer);
        return getDataTable(list);
    }

    /**
     * 导出回答列表
     */
    @PreAuthorize("@ss.hasPermi('system:answer:export')")
    @Log(title = "回答", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SkplAnswer skplAnswer)
    {
        List<SkplAnswer> list = skplAnswerService.selectSkplAnswerList(skplAnswer);
        ExcelUtil<SkplAnswer> util = new ExcelUtil<SkplAnswer>(SkplAnswer.class);
        util.exportExcel(response, list, "回答数据");
    }

    /**
     * 获取回答详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:answer:query')")
    @GetMapping(value = "/{answerId}")
    public AjaxResult getInfo(@PathVariable("answerId") String answerId)
    {
        return AjaxResult.success(skplAnswerService.selectSkplAnswerByAnswerId(answerId));
    }

    @PreAuthorize("@ss.hasPermi('system:answer:list')")
    @GetMapping(value = "/question/{questionId}")
    public TableDataInfo getInfoByQuestionId(@PathVariable("questionId") String questionId)
    {
        startPage();
        List<SkplAnswer> list = skplAnswerService.selectSkplAnswerListByQuestionId(questionId);
        return getDataTable(list);

    }

    /**
     * 新增回答
     */
    @PreAuthorize("@ss.hasPermi('system:answer:add')")
    @PostMapping(value = "/addto")
    public AjaxResult add(@RequestBody Map<String,String> map)
    {   String questionId=map.get("questionId");
        String answer=map.get("answer");
        SkplAnswer skplAnswer = new SkplAnswer();
        if(skplAnswer.getAnswerUser()==null) {
            SysUser user = SecurityUtils.getLoginUser().getUser();
            skplAnswer.setAnswerUser(user.getNickName());
            java.util.Date day =new Date();
            skplAnswer.setAnswerTime(day);
            skplAnswer.setAnswerContent(answer);
            skplAnswer.setAnswerQuestionId(questionId);
        }
        return toAjax(skplAnswerService.insertSkplAnswer(skplAnswer));
    }
    @PreAuthorize("@ss.hasPermi('system:answer:add')")
    @Log(title = "回答", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SkplAnswer skplAnswer)
    {
        return toAjax(skplAnswerService.insertSkplAnswer(skplAnswer));
    }

    /**
     * 修改回答
     */
    @PreAuthorize("@ss.hasPermi('system:answer:edit')")
    @Log(title = "回答", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SkplAnswer skplAnswer)
    {
        return toAjax(skplAnswerService.updateSkplAnswer(skplAnswer));
    }

    /**
     * 删除回答
     */
    @PreAuthorize("@ss.hasPermi('system:answer:remove')")
    @Log(title = "回答", businessType = BusinessType.DELETE)
	@DeleteMapping("/{answerIds}")
    public AjaxResult remove(@PathVariable String[] answerIds)
    {
        return toAjax(skplAnswerService.deleteSkplAnswerByAnswerIds(answerIds));
    }
}
